<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增调拨单')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-requisitio-add">
        	<h4 class="form-header h4">单据信息</h4>
            <input id="treeId" name="warehouseId" type="hidden" />
            <input id="totreeId" name="towarehouseId" type="hidden" />
            <div class="form-group">	
	            <label class="col-sm-1 control-label is-required">原仓库位置：</label>
	            <div class="col-sm-2">
	                <div class="input-group">
						<input class="form-control" type="text" required onclick="selectDeptTree()" id="treeName" readonly="true" th:value="${warehouse?.name}" >
					    <span class="input-group-addon"><i class="fa fa-search"></i></span>
					</div>
	            </div>
	            <label class="col-sm-1 control-label is-required">目标仓库位置：</label>
	            <div class="col-sm-2">
	                <div class="input-group">
						<input class="form-control" type="text" required onclick="toselectDeptTree()" id="totreeName" readonly="true" th:value="${towarehouse?.name}" >
					    <span class="input-group-addon"><i class="fa fa-search"></i></span>
					</div>
	            </div>
                <label class="col-sm-1 control-label">调拨单号：</label>
                <div class="col-sm-2">
                    <input name="requisitioNo" th:value="${requisitioNo}" class="form-control" type="text">
                </div>
                <label class="col-sm-1 control-label">调拨日期：</label>
                <div class="col-sm-2">
                    <div class="input-group date">
                        <input name="requisitioriq" th:value="${#dates.format(requisitioriq, 'yyyy-MM-dd')}" class="form-control" placeholder="yyyy-MM-dd" type="text">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-1 control-label">备注：</label>
                <div class="col-sm-11">
                    <textarea name="memo" class="form-control"></textarea>
                </div>
            </div>
            <h4 class="form-header h4">物料信息</h4>
            <div class="row">
                <div class="col-sm-12">
                    <button type="button" class="btn btn-white btn-sm" onclick="addSpkfkRow()"><i class="fa fa-plus"> 选择材料</i></button>
                    <button type="button" class="btn btn-white btn-sm" onclick="sub.delRow()"><i class="fa fa-minus"> 删除</i></button>
                    <div class="col-sm-12 select-table table-striped">
					    <table id="bootstrap-table"></table>
					</div>
                </div>
            </div>
        </form>
    </div>
    <div class="row">
        <div class="col-sm-offset-5 col-sm-10">
          	<button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>提 交</button>&nbsp;
          	<button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭 </button>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: bootstrap-fileinput-js"/>
    <script th:src="@{/js/math.min.js}"></script>
    <script th:src="@{/js/jquery.tmpl.js}"></script>
    <script th:inline="javascript">
        var prefix = ctx + "gcgl/requisitio"
        $("#form-requisitio-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.saveTab(prefix + "/add", $('#form-requisitio-add').serialize());
            }
        }

        $("input[name='requisitioriq']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });
        
        /*部门管理-新增-选择父部门树*/
		function selectDeptTree() {
			var treeId = $("#treeId").val();
			if ($.common.isEmpty(treeId)) {
				treeId = 0;
       		}
			var options = {
				title: '选择',
				width: "380",
				url: ctx + "gcgl/warehouse/selectTypeTree/" + treeId,
				callBack: doSubmit
			};
			$.modal.openOptions(options);
		}
		
		function doSubmit(index, layero){
			var body = $.modal.getChildFrame(index);
   			$("#treeId").val(body.find('#treeId').val());
   			$("#treeName").val(body.find('#treeName').val());
   			$.modal.close(index);
		}
		
		/*to部门管理-新增-选择父部门树*/
		function toselectDeptTree() {
			var treeId = $("#totreeId").val();
			if ($.common.isEmpty(treeId)) {
				treeId = 0;
       		}
			var options = {
				title: '选择',
				width: "380",
				url: ctx + "gcgl/warehouse/selectTypeTree/" + treeId,
				callBack: todoSubmit
			};
			$.modal.openOptions(options);
		}
		
		function todoSubmit(index, layero){
			var body = $.modal.getChildFrame(index);
   			$("#totreeId").val(body.find('#treeId').val());
   			$("#totreeName").val(body.find('#treeName').val());
   			$.modal.close(index);
		}
        
        /**商品单据**/
        $(function() {
	    	// 初始化数据, 可以由后台传过来
		    var options = {
                pagination: false,
		        showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: false,
                sidePagination: "client",
		        columns: [{
		            checkbox: true
		        },
		        {
		        	field: 'index',
		        	align: 'center',
		        	title: "序号",
                    formatter: function (value, row, index) {
                    	//var columnIndex = $.common.sprintf("<input type='hidden' name='items[%s].xuh' value='%s'>", $.table.serialNumber(index));
                    	var columnIndex = $.common.sprintf("<input type='hidden' name='index' value='%s'>", $.table.serialNumber(index));
                    	var columnId = $.common.sprintf("<input type='hidden' name='items[%s].jine' value='%s'>", 0);
                    	var columnZdjine = $.common.sprintf("<input type='hidden' class='form-control zdjinecount' name='items[%s].zdjine' value='%s'>", row.zdjine);
                    	return columnIndex + $.table.serialNumber(index) + columnId + columnZdjine;
                    }
                },
		        {
		            field: 'mingc',
		            align: 'center',
		            title: '物料名称',
		            formatter: function(value, row, index) {
		            	var html = $.common.sprintf("<input type='hidden' name='items[%s].mingc' value='%s'>", index, value);
		        		return html +value;
                    }
		        },{
		            field: 'clpp',
		            align: 'center',
		            title: '材料品牌',
		            formatter: function(value, row, index) {
		            	var html = $.common.sprintf("<input type='hidden' name='items[%s].clpp' value='%s'>", index, value);
		        		return html +value;
                    }
		        },{
		            field: 'guig',
		            align: 'center',
		            title: '规格型号',
		            formatter: function(value, row, index) {
		            	var html = $.common.sprintf("<input type='hidden' name='items[%s].guig' value='%s'>", index, value);
		        		return html +value;
                    }
		        },{
		            field: 'danw',
		            align: 'center',
		            title: '计量单位',
		            formatter: function(value, row, index) {
		            	var html = $.common.sprintf("<input type='hidden' name='items[%s].danw' value='%s'>", index, value);
		        		return html +value;
                    }
		        },{
		            field: 'kucshul',
		            align: 'center',
		            title: '库存数量',
		            formatter: function(value, row, index) {
		            	var html = $.common.sprintf("<input type='hidden' name='items[%s].kucshul' value='%s'>", index, value);
		        		return html +value;
                    }
		        },{
		            field: 'shul',
		            align: 'center',
		            title: '可调拨数量',
		            width: 120,
		            formatter: function(value, row, index) {
		            	var html = $.common.sprintf("<input required class='form-control shulcount' type='text' name='items[%s].shul' value='%s'>", index, value);
		        		return html;
                    }
		        },{
		            field: 'memo',
		            align: 'center',
		            title: '备注',
		            width: 200,
		            formatter: function(value, row, index) {
		            	var html = $.common.sprintf("<input class='form-control memocount' type='text' name='items[%s].memo' value='%s'>", index, value);
		        		return html;
                    }
		        },{
		        	field: 'id',
		            title: "操作",
		            align: "center",
		            width: 50,
		            formatter: function (value, row, index) {
		                var value = $.common.isNotEmpty(row.index)
		                  ? row.index
		                  : $.table.serialNumber(index);
		                return (
		                  '<a class="btn btn-danger btn-xs js-btn-delete" href="javascript:void(0)" onclick="removeGoods(\'' +
		                  value +
		                  '\', \'' +
		                  index +
		                  '\')"><i class="fa fa-remove"></i>删除</a>'
		                );
		            },
		        }]
		    };
		    $.table.init(options);
		    
		});
        
     	// 删除按钮事件
        function removeGoods(value, index){
      	  sub.delColumnByIndex(value);
        }

		function addSpkfkRow() {
			
			var warehoueId = $("#treeId").val();
			var options = {
		       title: '选择商品',
		       url: "/gcgl/requisitio/subdiaobo/"+warehoueId,
		       width: '1000',
		       height: '670',
		       callBack: doSubmitSpkfk
		    };
		    $.modal.openOptions(options);
		}
		
		function doSubmitSpkfk(index, layero){
            var rows = layero.find("iframe")[0].contentWindow.getSelections();
            if (rows.length == 0) {
                $.modal.alertWarning("请至少选择一条记录");
                return;
            }
            
            for( i=0;i<rows.length;i++){
            	
            	var flag = 0;
            	var count = $("#" + table.options.id).bootstrapTable('getData').length;
            	var lis = $("#" + table.options.id).bootstrapTable('getData');
            	//判断是否重复
            	for(j=0;j<count;j++){
            		if(lis[j].id==rows[i].id){
            			flag = 1;
            			break;
            		}
            	}
            	if(flag==0){
            		var row = {
            			xh:i+1,
            			materialId: rows[i].id,
            			mingc: rows[i].mingc,
            			guig: rows[i].guig,
            			danw: rows[i].danw,
            			zdjine: rows[i].zdjine,
            			kucshul: rows[i].kucshul-rows[i].linglCount,
            			shul: 0,
                        memo: rows[i].memo,
                        clpp: rows[i].clpp
                    }
                   	sub.addColumn(row);
            	}
            }
            
            $.modal.closeAll();
        }

    </script>
</body>
</html>